package com.xuelang.pipeline.artifact.domain.resource.service.impl;

import com.xuelang.pipeline.artifact.client.common.exception.BizException;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ToolChainQuery;
import com.xuelang.pipeline.artifact.domain.resource.ToolChain;
import com.xuelang.pipeline.artifact.domain.resource.repository.ToolChainRepository;
import com.xuelang.pipeline.artifact.domain.resource.service.ToolChainDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工具链领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ToolChainDomainServiceImpl implements ToolChainDomainService {

    @Autowired
    private ToolChainRepository toolChainRepository;

    @Override
    public PageRecord<ToolChain> pageList(long currentPage, long pageSize, ToolChainQuery toolChainQuery) {
        return toolChainRepository.pageList(currentPage, pageSize, toolChainQuery);
    }

    @Override
    public List<ToolChain> list(ToolChainQuery toolChainQuery) {
        return toolChainRepository.list(toolChainQuery);
    }
    @Override
    public Boolean save(ToolChain toolChain) {
        if(Boolean.TRUE.equals(existsVersion(toolChain.getCode(), toolChain.getType(), toolChain.getVersion()))) {
            throw new BizException("version已经存在");
        }
        //TODO,不同类型的version，需要验证是否合法。
        return toolChainRepository.save(toolChain);
    }

    @Override
    public Boolean update(ToolChain toolChain) {
        // 更新时，名称不能重复，摒弃工具链的名称。
        if(Boolean.TRUE.equals(existsVersion(toolChain.getCode(), toolChain.getType(), toolChain.getVersion()))) {
            throw new BizException("version已经存在");
        }
        return toolChainRepository.update(toolChain);
    }

    @Override
    public Boolean delete(String code) {
        return toolChainRepository.delete(code);
    }

    @Override
    public ToolChain query(String code) {
        return toolChainRepository.query(code);
    }

    @Override
    public Boolean existsVersion(String code, Integer type, String version) {
        return toolChainRepository.existsVersion(code, type, version);
    }

}
